package com.songlanyun.modules.offlineorder.entity;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.enums.OfflineOrderConstant;
import com.songlanyun.modules.comment.entity.OrderGoodsComment;
import com.songlanyun.modules.comment.service.OrderGoodsCommentService;
import com.songlanyun.modules.offlineorderincome.entity.OfflineOrderIncome;
import com.songlanyun.modules.offlineorderincome.service.OfflineOrderIncomeService;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import lombok.Builder;
import lombok.Data;

/**
 * 线下商家上报订单
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2023-02-11 16:57:11
 */
@Data
@TableName("yxt_offline_order")
public class OfflineOrder extends RootEntity<Long, OfflineOrder> implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 *
	 */
	@TableId
	private Long id;
	/**
	 * 会员Id
	 */
	private Long userId;
	/**
	 * 会员的手机号
	 */
	private String userMobile;
	/**
	 * 会员名称
	 */
	private String userName;
	/**
	 * 订单编号
	 */
	private String orderCode;
	/**
	 * 消费金额
	 */
	private BigDecimal salesMoney;
	/**
	 * 优惠比例
	 */
	private BigDecimal discountRatio;
	/**
	 * 优惠金额
	 */
	private BigDecimal discountMoney;
	/**
	 * 给用户的贡献值
	 */
	private BigDecimal giveUserCommissionAble;
	/**
	 * 给商家的贡献值
	 */
	private BigDecimal giveShopCommissionAble;
	/**
	 * 支付方式
	 */
	private OfflineOrderConstant.PayMode payMode;
	/**
	 * 订单备注
	 */
	private String remark;
	/**
	 * 商家上报订单前的积分
	 */
	private BigDecimal currentIntegralAble;
	/**
	 * 商家消耗（打赏）积分数
	 */
	private BigDecimal shopConsumeIntegralAble;
	/**
	 * 上报订单的商家id
	 */
	private Long shopId;
	/**
	 * 上报订单的商家名称
	 */
	private String shopName;
	/**
	 * 上报的时间
	 */
	private Date createTime;

	/**
	 * 是否被评论
	 */
	private Boolean comment;

	@Override
	public Long getIdentity() {
		return getId();
	}

	/**
	 * 店铺信息
	 */
	@TableField(exist = false)
	private ShopEntity shopEntity;

	@TableField(exist = false)
	private List<OfflineOrderIncome> incomes;

	@TableField(exist = false)
	private OrderGoodsComment memberComments;

	public void loadIncomes()
	{
		OfflineOrderIncomeService offlineOrderIncomeService = SpringUtil.getBean("offlineOrderIncomeService", OfflineOrderIncomeService.class);
		this.incomes = offlineOrderIncomeService.list(new LambdaQueryWrapper<OfflineOrderIncome>().eq(OfflineOrderIncome::getOfflineOrderId,this.id));
	}



	public void loadComments()
	{
		OrderGoodsCommentService orderGoodsCommentService = SpringUtil.getBean("orderGoodsCommentService", OrderGoodsCommentService.class);
		this.memberComments = orderGoodsCommentService.getMemberComments(CommentConstant.CommentType.UNION_BUSINESS, this.userId, String.valueOf(this.id));

	}

	public ShopEntity loadShop()
	{
		ShopService yxtShopService = SpringUtil.getBean("yxtShopService", ShopService.class);
		this.shopEntity = yxtShopService.loadShopBy(this.getShopId(),false);
		return this.shopEntity;
	}
}
